package com.inspur.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.inspur.common.core.domain.TreeSelect;
import com.inspur.common.utils.DateUtils;
import com.inspur.system.domain.PmAuth;
import com.inspur.system.domain.PmRole;
import com.inspur.system.domain.PmRoleAuth;
import com.inspur.system.domain.TreeSelectPmAuth;
import com.inspur.system.mapper.PmAuthMapper;
import com.inspur.system.service.IPmAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目权限Service业务层处理
 * 
 * @date 2024-04-22
 */
@Service
public class PmAuthServiceImpl implements IPmAuthService
{
    @Autowired
    private PmAuthMapper pmAuthMapper;

    /**
     * 查询项目权限
     * 
     * @param authId 项目权限主键
     * @return 项目权限
     */
    @Override
    public PmAuth selectPmAuthByAuthId(String authId)
    {
        return pmAuthMapper.selectPmAuthByAuthId(authId);
    }

    /**
     * 查询项目权限列表
     * 
     * @param pmAuth 项目权限
     * @return 项目权限
     */
    @Override
    public List<PmAuth> selectPmAuthList(PmAuth pmAuth)
    {
        return pmAuthMapper.selectPmAuthList(pmAuth);
    }

    /**
     * 新增项目权限
     * 
     * @param pmAuth 项目权限
     * @return 结果
     */
    @Override
    public int insertPmAuth(PmAuth pmAuth)
    {
        pmAuth.setCreateTime(DateUtils.getNowDate());
        return pmAuthMapper.insertPmAuth(pmAuth);
    }

    /**
     * 修改项目权限
     * 
     * @param pmAuth 项目权限
     * @return 结果
     */
    @Override
    public int updatePmAuth(PmAuth pmAuth)
    {
        pmAuth.setUpdateTime(DateUtils.getNowDate());
        return pmAuthMapper.updatePmAuth(pmAuth);
    }

    /**
     * 批量删除项目权限
     * 
     * @param authIds 需要删除的项目权限主键
     * @return 结果
     */
    @Override
    public int deletePmAuthByAuthIds(String[] authIds)
    {
        return pmAuthMapper.deletePmAuthByAuthIds(authIds);
    }

    /**
     * 删除项目权限信息
     * 
     * @param authId 项目权限主键
     * @return 结果
     */
    @Override
    public int deletePmAuthByAuthId(String authId)
    {
        return pmAuthMapper.deletePmAuthByAuthId(authId);
    }

    /**
     * 根据用户查询系统菜单列表
     *
     * @param userId 用户ID
     * @return 菜单列表
     */
    @Override
    public List<PmAuth> selectMenuList(Long userId,String cProNo)
    {
        PmAuth pmAuth = new PmAuth();
        pmAuth.getParams().put("userId",userId);
        pmAuth.getParams().put("cProNo",cProNo);
        List<PmAuth> menuList = null;
        menuList = pmAuthMapper.selectMenuListByUserId(pmAuth);
        return menuList;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param menus 菜单列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelectPmAuth> buildMenuTreeSelect(List<PmAuth> menus)
    {
        List<PmAuth> menuTrees = buildMenuTree(menus);
        return menuTrees.stream().map(TreeSelectPmAuth::new).collect(Collectors.toList());
    }


    /**
     * 构建前端所需要树结构
     *
     * @param menus 菜单列表
     * @return 树结构列表
     */
    @Override
    public List<PmAuth> buildMenuTree(List<PmAuth> menus)
    {
        List<PmAuth> returnList = new ArrayList<PmAuth>();
        List<String> tempList = menus.stream().map(PmAuth::getAuthId).collect(Collectors.toList());
        for (Iterator<PmAuth> iterator = menus.iterator(); iterator.hasNext();)
        {
            PmAuth menu = (PmAuth) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentId()))
            {
                recursionFn(menus, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = menus;
        }
        return returnList;
    }



    /**
     * 根据角色ID查询菜单树信息
     *
     * @param roleId 角色ID
     * @return 选中菜单列表
     */
    @Override
    public List<String> selectMenuListByRoleId(String roleId)
    {
        PmRole role = pmAuthMapper.selectRoleById(roleId);
        return pmAuthMapper.selectMenuListByRoleId(roleId, role.isMenuCheckStrictly());
    }

    /**
     * 修改保存角色信息
     *
     * @param role 角色信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateRole(PmRole role)
    {
        /*// 修改角色信息
        pmAuthMapper.updateRole(role);*/
        // 删除角色与菜单关联
        pmAuthMapper.deleteRoleAuthByRoleId(role.getRoleId());
        return insertRoleMenu(role);
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<PmAuth> list, PmAuth t)
    {
        List<PmAuth> tlist = new ArrayList<PmAuth>();
        Iterator<PmAuth> it = list.iterator();
        while (it.hasNext())
        {
            PmAuth n = (PmAuth) it.next();
            if (Long.parseLong(n.getParentId()) == Long.parseLong(t.getAuthId()))
            {
                tlist.add(n);
            }
        }
        return tlist.size() > 0;
    }



    /**
     * 递归列表
     *
     * @param list 分类表
     * @param t 子节点
     */
    private void recursionFn(List<PmAuth> list, PmAuth t)
    {
        // 得到子节点列表
        List<PmAuth> childList = getChildList(list, t);
        t.setChildren(childList);
        for (PmAuth tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<PmAuth> getChildList(List<PmAuth> list, PmAuth t)
    {
        List<PmAuth> tlist = new ArrayList<PmAuth>();
        Iterator<PmAuth> it = list.iterator();
        while (it.hasNext())
        {
            PmAuth n = (PmAuth) it.next();
            if (Long.parseLong(n.getParentId()) == Long.parseLong(t.getAuthId()))
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 新增角色菜单信息
     *
     * @param role 角色对象
     */
    public int insertRoleMenu(PmRole role)
    {
        int rows = 1;
        // 新增用户与角色管理
        List<PmRoleAuth> list = new ArrayList<PmRoleAuth>();
        for (String menuId : role.getMenuIds())
        {
            PmRoleAuth rm = new PmRoleAuth();
            rm.setRoleId(role.getRoleId());
            rm.setAuthId(menuId);
            list.add(rm);
        }
        if (list.size() > 0)
        {
            rows = pmAuthMapper.batchRoleMenu(list);
        }
        return rows;
    }
}
